package cn.hpu.mobile_bank.service.impl;

import cn.hpu.mobile_bank.domain.Account;
import cn.hpu.mobile_bank.domain.Recipient;
import cn.hpu.mobile_bank.dto.req.AddOrUpdateRecipientReq;
import cn.hpu.mobile_bank.dto.req.RequestInfo;
import cn.hpu.mobile_bank.dto.resp.ApiResult;
import cn.hpu.mobile_bank.exception.BusinessException;
import cn.hpu.mobile_bank.mapper.AccountMapper;
import cn.hpu.mobile_bank.mapper.RecipientMapper;
import cn.hpu.mobile_bank.service.RecipientService;
import cn.hpu.mobile_bank.utils.RequestHolder;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author DELL
 * @description 针对表【recipient】的数据库操作Service实现
 * @createDate 2024-06-19 15:14:37
 */
@Service
public class RecipientServiceImpl extends ServiceImpl<RecipientMapper, Recipient>
        implements RecipientService {

    @Autowired
    private RecipientMapper recipientMapper;

    @Autowired
    private AccountMapper accountMapper;

    @Override
    public ApiResult<List<Recipient>> listByTel() {
        RequestInfo requestInfo = RequestHolder.get();
        String telephone = requestInfo.getTelephone();
        List<Recipient> list = recipientMapper.selectByTelephone(telephone);
        return ApiResult.success(list);
    }

    @Override
    public ApiResult<String> addRecipient(AddOrUpdateRecipientReq addOrUpdateRecipientReq) {
        RequestInfo requestInfo = RequestHolder.get();
        String telephone = requestInfo.getTelephone();
        //校验收款人信息
        validateRecipient(addOrUpdateRecipientReq, telephone);
        //添加收款人
        recipientMapper.addRecipient(addOrUpdateRecipientReq.getRecepAccount(), addOrUpdateRecipientReq.getRecepName(), telephone);
        return ApiResult.success();
    }

    @Override
    public ApiResult<String> updateRecipient(AddOrUpdateRecipientReq addOrUpdateRecipientReq) {
        RequestInfo requestInfo = RequestHolder.get();
        String telephone = requestInfo.getTelephone();
        //校验收款人信息
        validateRecipient(addOrUpdateRecipientReq, telephone);
        //删除旧收款人信息
        recipientMapper.deleteRecipient(addOrUpdateRecipientReq.getRecepAccount(), addOrUpdateRecipientReq.getRecepName(), telephone);
        //添加收款人
        recipientMapper.addRecipient(addOrUpdateRecipientReq.getRecepAccount(), addOrUpdateRecipientReq.getRecepName(), telephone);
        return ApiResult.success();

    }

    private void validateRecipient(AddOrUpdateRecipientReq addOrUpdateRecipientReq, String telephone) {
        //添加/修改的收款方不能是自己的账户
        //查询自己的账户
//        List<Account> accounts = accountMapper.selectByTelephone(telephone);
//        for (Account account : accounts) {
//            if (account.getAccountName().equals(addOrUpdateRecipientReq.getRecepAccount())) {
//                throw new BusinessException("收款人不能是自己的账户");
//            }
//        }

        //验证收款人和账户是否存在
        Account account = accountMapper.selectByPersonNameAndAccountName(addOrUpdateRecipientReq.getRecepAccount(), addOrUpdateRecipientReq.getRecepName());
        if (account == null) throw new BusinessException("收款人不存在");

        //验证收款人是否添加过了
        Recipient recipient1 = recipientMapper.selectByThree(addOrUpdateRecipientReq.getRecepAccount(), addOrUpdateRecipientReq.getRecepName(), telephone);
        if (recipient1 != null) throw new BusinessException("收款人已添加");
    }


}




